package cn.song.freight.domain.result;

import cn.song.freight.domain.query.QueryEntity;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.List;


/**
 * 分页工具类
 *
 * @author SongRenShuo
 * @since 2025-02-12 10:09:24
 */
@Data
@AllArgsConstructor
@ApiModel(description = "分页工具类")
public class PageUtils<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 当前页码
     */
    @ApiModelProperty(value = "当前页码", example = "1")
    private int currentPage;
    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页条数", example = "10")
    private int pageSize;
    /**
     * 记录总数
     */
    @ApiModelProperty(value = "记录总数", example = "100")
    private long totalSize;
    /**
     * 页码总数
     */
    @ApiModelProperty(value = "页码总数", example = "10")
    private int totalPages;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private List<T> list;


    /**
     * 自动封装
     *
     * @param list      列表
     * @return {@link PageUtils }
     */
    public static <T> PageUtils<T> getPageInfo(List<T> list, QueryEntity queryEntity) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        PageUtils<T> pageUtils = new PageUtils<>();
        pageUtils.setTotalSize(pageInfo.getTotal());
        pageUtils.setTotalPages(pageInfo.getPages());
        pageUtils.setList(pageInfo.getList());
        pageUtils.setCurrentPage(queryEntity.getCurrentPage());
        pageUtils.setPageSize(queryEntity.getPageSize());
        PageHelper.clearPage();
        return pageUtils;
    }
    /**
     * 自动封装
     *
     * @param list      列表
     * @return {@link PageUtils }
     */
    public static <T> PageUtils<T> getPageInfo(List<T> list, Page<T> page) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        PageUtils<T> pageUtils = new PageUtils<>();
        pageUtils.setTotalSize(pageInfo.getTotal());
        pageUtils.setTotalPages(pageInfo.getPages());
        pageUtils.setList(pageInfo.getList());
        pageUtils.setCurrentPage((int) page.getCurrent());
        pageUtils.setPageSize((int) page.getSize());
        PageHelper.clearPage();
        return pageUtils;
    }

    /**
     * 自动封装
     *
     * @param list      列表
     * @param pageUtils 页面实用程序
     * @return {@link PageUtils }
     */
    public static <T> PageUtils<T> getPageInfo(List<T> list, PageUtils<T> pageUtils) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        pageUtils.setTotalSize(pageInfo.getTotal());
        pageUtils.setTotalPages(pageInfo.getPages());
        pageUtils.setList(pageInfo.getList());
        PageHelper.clearPage();
        return pageUtils;
    }

    /**
     * 自动封装
     *
     * @param list      列表
     * @param pageUtils 页面实用程序
     * @return {@link PageUtils }
     */
    public static <T> PageUtils<T> getPageInfo(List<T> list, PageUtils<T> pageUtils, Integer total) {
        int pageSize = pageUtils.getPageSize();

        pageUtils.setTotalSize(total);
        int totalPage = (total + pageSize - 1) / pageSize;
        pageUtils.setTotalPages(totalPage);
        pageUtils.setList(list);

        // 当total为0或pageSize过大导致totalPage为0时，可以设置一个默认值
        if (total == 0 || totalPage == 0) {
            pageUtils.setTotalPages(1);
        }

        return pageUtils;
    }

    /**
     * 转换分页对象 Page->PageUtils
     *
     * @param page 分页对象
     * @return {@link PageUtils }<{@link T }>
     */
    public static <T> PageUtils<T> getPageUtils(Page<T> page) {
        PageUtils<T> pageUtils = new PageUtils<>();
        pageUtils.setCurrentPage((int) page.getCurrent());
        pageUtils.setPageSize((int) page.getSize());
        pageUtils.setTotalSize(page.getTotal());
        pageUtils.setTotalPages((int) page.getPages());
        pageUtils.setList(page.getRecords());
        return pageUtils;
    }


    /**
     * 分页构造
     *
     * @param currentPage 当前页
     * @param pageSize    条数
     */
    public PageUtils(Integer currentPage, Integer pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    /**
     * 默认值
     */
    public PageUtils() {
        this.currentPage = 1;
        this.pageSize = 10;
    }

}

